"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库兼容--兼容b库函数
Case Name   : TIMEDIFF参数值微秒进位测试
Create At   : 2023.01.04
Owner       : z@wan005
Description :
    1.创建兼容b库
    2.参数值含微秒
    3.清理环境
Expect      :
    1.创建成功
    2.成功（小数末尾0不显示）
    3.清理环境完成
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Compatibility(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.db_name = 'db_timediff_case0007'

    def test_grammar(self):
        text = '---step1:创建兼容b库;expect:创建成功---'
        self.log.info(text)
        sql_cmd1 = f"drop database if exists {self.db_name};" \
                   f"create database {self.db_name} dbcompatibility ='b';"
        sql_res = self.pri_sh.execut_db_sql(sql_cmd1)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败:' + text)

        text = '---step2:参数值含微秒;expect:成功（小数末尾0不显示）---'
        self.log.info(text)
        sql_cmd2 = "select TIMEDIFF('03:59:00.0000001', '02:00:00.999999');" \
                   "select TIMEDIFF('03:59:00.0000005', '02:00:00.000009');" \
                   "select TIMEDIFF" \
                   "('2022-11-23 14:25:56.0000005', '2022-11-23 14:25:56');" \
                   "select TIMEDIFF" \
                   "('2022-11-23 14:25:56.9999999', '2022-11-23 14:25:56');"
        sql_res = self.pri_sh.execut_db_sql(sql_cmd2,
                                            dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 19, '行数不到21行')
        self.assertEqual('01:58:59.000001', sql_res.splitlines()[2].strip(),
                         '执行失败:' + text)
        self.assertEqual('01:58:59.999992', sql_res.splitlines()[7].strip(),
                         '执行失败:' + text)
        self.assertEqual('00:00:00.000001', sql_res.splitlines()[-7].strip(),
                         '执行失败:' + text)
        self.assertEqual('00:00:01', sql_res.splitlines()[-2].strip(),
                         '执行失败:' + text)

    def tearDown(self):
        text = '---step3:清理环境;expect:清理环境完成---'
        self.log.info(text)
        drop_msg = self.pri_sh.execut_db_sql(f"drop database if exists "
                                             f"{self.db_name};")
        self.log.info(drop_msg)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_msg,
                      '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
